package oriol.forns.gutierrez;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class EditorBack extends SurfaceView{

	private EditorThread bgdt;
	private SurfaceHolder holder;
	private Paint fons;
	private Shader shader;
	public ArrayList<Integer> elements;
	private ArrayList<Objecte> belements;
	private ArrayList<Bitmap> bitmaps;
	private BitmapFactory.Options options;
	private int pose = 1;
	private int posb = 0;
	
	private boolean surfacecreated;
	private boolean threadrunning;
	
	
	public EditorBack(Context context) {
		super(context);
		init();
	}
	
	public EditorBack(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public EditorBack(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		surfacecreated = false;
		threadrunning = false;
		holder = getHolder();
		holder.addCallback(new Callback() {
			
			public void surfaceDestroyed(SurfaceHolder holder) {
				stopthread();
				surfacecreated = false;
			}
			
			public void surfaceCreated(SurfaceHolder holder) {
				holder.setFormat(PixelFormat.RGBA_8888);
				
				fons = new Paint();
				belements = new ArrayList<Objecte>();
				bitmaps = new ArrayList<Bitmap>();
				elements = new ArrayList<Integer>();
				
				loadbitmaps();
				
				
				shader = new RadialGradient(0, 0, getHeight()/2, 0xFFFFFFFF, 0xFFA4D8FF, Shader.TileMode.CLAMP);
				fons.setShader(shader);
				
				for (int i = 0; i < 287; ++i) {
					elements.add(-1);
					++i;
					for (int j = 0; j < 16; ++j) {
						elements.add(8);
						++i;
					}
					elements.add(-1);
				}
				
				for(int i=0; i<elements.size(); i++) {
					if (elements.get(i) != -1) {
						float tx = (float)(GV.Screen.metrics.widthPixels*0.055);
						float ty = (float)((GV.Screen.metrics.heightPixels/1.5)*0.055);
						float x = tx*(i%18);
						float y = ty*(i/18)+ty;
						belements.add(new Objecte(x, y, 0, 0, tx, ty, bitmaps.get(elements.get(i) ) ) );
					}
				}
				
				Objecte o = belements.get(posb);
				o.nouimg(bitmaps.get(0));
				belements.set(posb, o);
				
				surfacecreated = true;
			}
			
			public void surfaceChanged(SurfaceHolder holder, int format, int width,	int height) {
				startthread();
			}
		});
	}
	
	private void loadbitmaps() {
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.blanc, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.blau, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.groc, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.marro, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.negre, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.taronja, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.vermell, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.vert, options));
		bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.transpa, options));
	}
	
	public void confirmar(int elm) {
		elements.set(pose, elm);
		Log.v("color", String.valueOf(pose));
	}
	
	public void moub(int x, int elm){
		//vell
		Objecte o = belements.get(posb);
		o.nouimg(bitmaps.get(elements.get(pose)));
		belements.set(posb, o);
		
		posb +=x;
		if (posb >= 257) posb = 256;
		if (posb < 0) posb = 0;
			
		//actual
		o = belements.get(posb);
		o.nouimg(bitmaps.get(elm));
		belements.set(posb, o);
	}
	
	public void moue(int x, int elm){
		while (x > 0 || x < 0 || elements.get(pose) == -1) {
			if (x > 0) {
				pose++;
				if (pose >= 287) pose = 286;
				if (x != 0) --x;
			}
			else {
				pose--;
				if (pose < 1) pose = 1;
				if (x != 0) ++x;
			}
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawRect(0, 0, getWidth(), getHeight(), fons);
		for (int i = 0; i < belements.size(); ++i) {
			belements.get(i).draw(canvas);
		}
	}
	
	public void startthread(){
		if(!surfacecreated) return;
		if(threadrunning) return;
		bgdt = new EditorThread(this);
		bgdt.setRunning(true);
		bgdt.start();
		threadrunning = true;
	}
	
	public void stopthread() {
		if(!surfacecreated) return;
		if(!threadrunning) return;
		boolean retry = true;
		while (retry) {
			try {
				bgdt.setRunning(false);
				bgdt.join();
				threadrunning = false;
				retry = false;
				
			} catch (InterruptedException e) {}
		}
	}
}
